<!doctype html>
<html>
<head>
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
  <title>mqtt-connection-basic</title>

  <script src="../../webcomponentsjs/webcomponents.min.js"></script>
  <script src="../../web-component-tester/browser.js"></script>
  <script src="../../test-fixture/test-fixture-mocha.js"></script>

  <link rel="import" href="../../polymer/polymer.html">
  <link rel="import" href="../../promise-polyfill/promise-polyfill-lite.html">
  <link rel="import" href="../../test-fixture/test-fixture.html">

  <!-- Step 1: import the element to test -->
  <link rel="import" href="../mqtt-elements.html">
</head>
<body>

<test-fixture id="AutoConnection">
  <template>
    <mqtt-connection></mqtt-connection>
  </template>
</test-fixture>

<test-fixture id="NoAutoConnection">
  <template>
    <mqtt-connection id="noAuto"></mqtt-connection>
  </template>
</test-fixture>

<test-fixture id="MaxListeners">
  <template>
    <mqtt-connection max-listeners="15"></mqtt-connection>
  </template>
</test-fixture>

<test-fixture id="WithCredentials">
  <template>
    <mqtt-connection auto with-credentials></mqtt-connection>
  </template>
</test-fixture>

<test-fixture id="WithCredentialsNoAuto">
  <template>
    <mqtt-connection with-credentials></mqtt-connection>
  </template>
</test-fixture>


<script>
  //  Setp 2: get the element
  var mqttElement;
  var mqttDisabledAuto;
  var mqttMaxListeners;
  var mqttWithCredentials;

  suite('<mqtt-connection>', function () {


    suite('mqtt-connection-spec', function () {
      setup(function () {
        mqttElement = fixture('AutoConnection');
      });

      //  Setup 3: Test the element
      test('Element is not null', function () {
        assert.isNotNull(mqttElement);
        assert.notTypeOf(mqttElement, 'undefined');
      });

      test('mqtt-connection.clean is boolean', function () {
        assert.isBoolean(mqttElement.clean, 'mqtt-connection.options.clean should be a number greater than 0');
        assert.isTrue(mqttElement.clean);
      });

      test('mqtt-connection.connected is boolean', function () {
        assert.isBoolean(mqttElement.connected, 'mqtt-connection.connected should be a boolean');
        assert.isFalse(mqttElement.connected);
      });

      test('mqtt-connection.disconnecting is booelan and false', function () {
        assert.isBoolean(mqttElement.disconnecting, 'mqtt-connection.disconnecting should be a boolean');
        assert.isFalse(mqttElement.disconnecting);
      });

      test('mqtt-connection.auto is boolean and false', function () {
        assert.isBoolean(mqttElement.auto, 'mqtt-connection.auto should be a boolean');
        assert.isFalse(mqttElement.auto);
      });

      test('mqtt-connection.auto is false', function () {
        assert.isBoolean(mqttElement.auto, 'mqtt-connection.auto should be a boolean');
        assert.isFalse(mqttElement.auto);
        mqttElement.auto = true;
        assert.isTrue(mqttElement.auto);
      });

      test('mqtt-connection.clientIdPrefix is String', function () {
        assert.typeOf(mqttElement.clientIdPrefix, 'string', 'mqtt-connection.clientIdPrefix should be a String');
        assert.strictEqual(mqttElement.clientIdPrefix, 'mqttjs_', 'On a new instance of <mqtt-connection> the clientIdPrefix should be mqttjs_');
      });

      test('mqtt-connection.options.protocolId is a String', function () {
        assert.typeOf(mqttElement.options.protocolId, 'string', 'mqtt-connection.options.protocolId should be a String');
        assert.strictEqual(mqttElement.options.protocolId, 'MQTT', 'On a new instance of <mqtt-connection> the protocolId should be MQIsdp');
      });

      test('mqtt-connection.options.protocolVersion is a number', function () {
        assert.isNumber(mqttElement.options.protocolVersion, 'mqtt-connection.options.protocolVersion should be a number greater than 0');
        assert.strictEqual(mqttElement.options.protocolVersion, 4, 'On a new instance of <mqtt-connection> the protocolVersion should be 4');
      });

      test('mqtt-connection.options.reconnectPeriod is a number', function () {
        assert.isNumber(mqttElement.options.reconnectPeriod, 'mqtt-connection.options.reconnectPeriod should be a number greater than 0');
        assert.strictEqual(mqttElement.options.reconnectPeriod, 1000, 'On a new instance of <mqtt-connection> the reconnectPeriod should be 4');
      });

      test('mqtt-connection.options.keepalive is a number', function () {
        assert.isNumber(mqttElement.options.keepalive, 'mqtt-connection.options.keepalive should be a number greater than 0');
        assert.strictEqual(mqttElement.options.keepalive, 120);
      });

      test('mqtt-connection.options.clientId should be a String', function () {
        assert.typeOf(mqttElement.options.clientId, 'string', 'mqtt-connection.options.clientId should be a string');
      });

      test('mqtt-connection.options.will should be undefined', function () {
        assert.typeOf(mqttElement.options.will, 'undefined', 'mqtt-connection.options.will should be a undefined on a new instance of <mqtt-connection>');
      });

      test('mqtt-connection.options.encoding is a String', function () {
        assert.typeOf(mqttElement.options.encoding, 'string', 'mqtt-connection.options.encoding should be a string');
      });

      test('mqtt-connection.options.username should be undefined', function () {
        assert.typeOf(mqttElement.options.username, 'undefined', 'mqtt-connection.options.username should be undefined');
      });

      test('mqtt-connection.username should be undefined', function () {
        assert.typeOf(mqttElement.username, 'undefined', 'mqtt-connection.options.username should be undefined');
      });

      test('setting mqtt-connection.username should be reflected to mqtt-connection.options.username', function () {
        assert.typeOf(mqttElement.username, 'undefined', 'mqtt-connection.options.username should be undefined');
        mqttElement.username = "mqtt";
        assert.typeOf(mqttElement.username, 'string', 'mqtt-connection.options.username should be undefined');
        expect(mqttElement.username).to.equal("mqtt");
        expect(mqttElement.options.username).to.equal("mqtt");
      });

      test('setting mqtt-connection.options.username should not be reflected to mqtt-connection.username', function () {
        assert.typeOf(mqttElement.username, 'undefined', 'mqtt-connection.options.username should be undefined');
        mqttElement.options.username = "mqtt";
        assert.typeOf(mqttElement.options.username, 'string', 'mqtt-connection.options.username should be undefined');
        assert.typeOf(mqttElement.username, 'undefined', 'mqtt-connection.options.username should be undefined');
        expect(mqttElement.options.username).to.equal("mqtt");
      });

      test('mqtt-connection.options.password should be undefined', function () {
        assert.typeOf(mqttElement.options.password, 'undefined', 'mqtt-connection.options.password should be undefined');
      });

      test('mqtt-connection.password should be undefined', function () {
        assert.typeOf(mqttElement.password, 'undefined', 'mqtt-connection.options.password should be undefined');
      });


      test('setting mqtt-connection.password should be reflected to mqtt-connection.options.password', function () {
        assert.typeOf(mqttElement.password, 'undefined', 'mqtt-connection.options.username should be undefined');
        mqttElement.password = ".js";
        assert.typeOf(mqttElement.password, 'string', 'mqtt-connection.options.username should be undefined');
        expect(mqttElement.password).to.equal(".js");
        expect(mqttElement.options.password).to.equal(".js");
      });

      test('setting mqtt-connection.options.password should not be reflected to mqtt-connection.password', function () {
        assert.typeOf(mqttElement.password, 'undefined', 'mqtt-connection.options.username should be undefined');
        mqttElement.options.password = ".js";
        assert.typeOf(mqttElement.options.password, 'string', 'mqtt-connection.options.username should be undefined');
        expect(mqttElement.options.password).to.equal(".js");
        assert.typeOf(mqttElement.password, 'undefined', 'mqtt-connection.options.password should be undefined');
      });

      test('mqtt-connection.withCredentials is boolean and false by default', function () {
        expect(mqttElement.withCredentials).to.be.a("boolean");
        expect(mqttElement.withCredentials).to.be.false;
      });

      test('mqtt-connection.url is a String', function () {
        assert.typeOf(mqttElement.url, 'string', 'mqtt-connection.url should be a string');
      });
    });

    suite('mqtt-conntection-no-auto-spec', function () {
      setup(function () {
        mqttDisabledAuto = fixture('NoAutoConnection');
      });

      test('mqtt-connection#auto.auto is boolean', function () {
        assert.isBoolean(mqttDisabledAuto.auto, 'mqtt-connection.auto should be a boolean');
        assert.isFalse(mqttDisabledAuto.auto);
      });
    });

    suite('mqtt-conntection-with-increased-max-listeners', function () {
      setup(function () {
        mqttMaxListeners = fixture('MaxListeners');
      });

      test('mqtt-connection#maxListeners is number', function () {
        assert.isNumber(mqttMaxListeners.maxListeners, 'mqtt-connection.maxListeners should be a number');
        assert.strictEqual(mqttMaxListeners.maxListeners, 15);
        mqttMaxListeners.maxListeners = 30;
        assert.strictEqual(mqttMaxListeners.maxListeners, 30);
      });
    });

    suite('mqtt-conntection-withCredentials', function () {
      setup(function () {
        mqttWithCredentials = fixture('WithCredentials');
      });

      test('mqtt-connection with with-credentials and auto will wait with connect until ' +
          '( mqtt-connection.username || mqtt-connection.options.username ) &&' +
          ' ( mqtt-connection.password || mqtt-connection.options.password ) is set', function () {
        expect(mqttWithCredentials.client).to.be.undefined;
        mqttWithCredentials.username = "mqtt";
        expect(mqttWithCredentials.client).to.be.undefined;
        mqttWithCredentials.password = ".js";
        expect(mqttWithCredentials.client).to.be.an("object");
      });
    });

    suite('mqtt-conntection-withCredentials and noAuto', function () {
      setup(function () {
        mqttWithCredentials = fixture('WithCredentialsNoAuto');
      });

      test('mqtt-connection with with-credentials and no auto will wait with connect until ' +
          '( mqtt-connection.username || mqtt-connection.options.username ) &&' +
          ' ( mqtt-connection.password || mqtt-connection.options.password ) is set and auto is set to true', function (done) {

        mqttWithCredentials.addEventListener("mqtt-connection-offline",function(){
          expect(mqttWithCredentials.client).to.be.an("object");
          done();
        });

        expect(mqttWithCredentials.client).to.be.undefined;
        mqttWithCredentials.username = "mqtt";
        expect(mqttWithCredentials.client).to.be.undefined;
        mqttWithCredentials.password = ".js";
        expect(mqttWithCredentials.client).to.be.undefined;

        mqttWithCredentials.auto = true;

      });
    });





  });
</script>

</body>
</html>
